5 ~ 4, #Conservative
6 ~ 5, #Moderately Conservative
7 ~ 6, #Extremely Conservative
)) %>%
mutate(
exefficacy1 = exefficacy1 %>%
incase::switch_case(
1 ~ 0, #Not at all important
2 ~ 1, #slightly important
3 ~ 2, #moderately important
4 ~ 3, #very important
5 ~ 4, #extremely important
)) %>%
mutate(
exefficacy2 = exefficacy2 %>%
incase::switch_case(
1 ~ 0, #Not at all important
2 ~ 1, #slightly important
3 ~ 2, #moderately important
4 ~ 3, #very important
5 ~ 4, #extremely important
)) %>%
mutate(
linkedfate = linkedfate%>%
incase::switch_case(
1 ~ 0, #No, not at at all
2 ~ 1, #yes, a little
3 ~ 2, #yes, somewhat
4 ~ 3, #yes, a lot
)) %>%
mutate(
groupefficacy1 = groupefficacy1 %>%
incase::switch_case(
1 ~ 0, #Never
2 ~ 1, #Some of the time
3 ~ 2, #About half the time
4 ~ 3, #much of the time
5 ~ 4, #all of the time
)) %>%
mutate(
groupefficacy2 = groupefficacy2 %>%
incase::switch_case(
1 ~ 0, #Never
2 ~ 1, #Some of the time
3 ~ 2, #About half the time
4 ~ 3, #much of the time
5 ~ 4, #all of the time
))
#creating age categories
clean <- clean %>%
mutate(
age = age + 17,
# Create categories
age_group = dplyr::case_when(
age >= 18 & age <= 25 ~ "18-25",
age > 25 & age <= 29 ~ "25-29",
age > 29 & age <= 34 ~ "29-34",
age > 35 & age <= 44 ~ "35-44",
age > 45 & age <= 54 ~ "45-54",
age > 55 & age <= 64 ~ "55-64",
age > 64 & age <= 98 ~ "64-98"
),
# Convert to factor
age_group = factor(
age_group,
level = c( "18-25",
"25-29",
"29-34",
"35-44",
"45-54",
"55-64",
"64-98")
)
)
#characterizing the sample
clean %>%
ggplot(mapping = aes(x = age_group)) +
geom_bar(width = 0.6)
summary(clean$age)
summary(clean$age_group)
#cleaning data and creating variables
clean = hoff_data_raw %>%
mutate(both_na = is.na(C1_manip) + is.na(C2_manip)) %>%
filter(both_na != 2) %>%
filter(!(C1_manip %in% c(2,3)) & !(C2_manip %in% c(2,3))) %>%
mutate(treat = if_else(is.na(C1_manip), 0,1)) %>%
filter(race == 3) %>%
mutate(
Female = gender %>%
incase::switch_case(
1 ~ 0, #men
2 ~ 1, #women
)) %>%
mutate(
pid = pid %>%
incase::switch_case(
1 ~ 0, #Republican
2 ~ 1, #Democrat
3 ~ 2, #Independent
4 ~ 3, #Other
5 ~ 4, #No Preference
)) %>%
mutate(
democrat = if_else(pid %in% c(1), 1, 0)) %>%
mutate(
ideology_1 = ideology_1 %>%
incase::switch_case(
1 ~ 0, #Extremely Liberal
2 ~ 1, #Moderately Liberal
3 ~ 2, #Liberal
4 ~ 3, #Moderate
5 ~ 4, #Conservative
6 ~ 5, #Moderately Conservative
7 ~ 6, #Extremely Conservative
)) %>%
mutate(
exefficacy1 = exefficacy1 %>%
incase::switch_case(
1 ~ 0, #Not at all important
2 ~ 1, #slightly important
3 ~ 2, #moderately important
4 ~ 3, #very important
5 ~ 4, #extremely important
)) %>%
mutate(
exefficacy2 = exefficacy2 %>%
incase::switch_case(
1 ~ 0, #Not at all important
2 ~ 1, #slightly important
3 ~ 2, #moderately important
4 ~ 3, #very important
5 ~ 4, #extremely important
)) %>%
mutate(
linkedfate = linkedfate%>%
incase::switch_case(
1 ~ 0, #No, not at at all
2 ~ 1, #yes, a little
3 ~ 2, #yes, somewhat
4 ~ 3, #yes, a lot
)) %>%
mutate(
groupefficacy1 = groupefficacy1 %>%
incase::switch_case(
1 ~ 0, #Never
2 ~ 1, #Some of the time
3 ~ 2, #About half the time
4 ~ 3, #much of the time
5 ~ 4, #all of the time
)) %>%
mutate(
groupefficacy2 = groupefficacy2 %>%
incase::switch_case(
1 ~ 0, #Never
2 ~ 1, #Some of the time
3 ~ 2, #About half the time
4 ~ 3, #much of the time
5 ~ 4, #all of the time
))
#creating age categories
clean <- clean %>%
mutate(
age = age + 17,
# Create categories
age_group = dplyr::case_when(
age >= 18 & age <= 25 ~ "18-25",
age > 25 & age <= 29 ~ "25-29",
age > 29 & age <= 34 ~ "29-34",
age > 35 & age <= 44 ~ "35-44",
age > 45 & age <= 54 ~ "45-54",
age > 55 & age <= 64 ~ "55-64",
age > 64 & age <= 98 ~ "64-98"
)#,
# Convert to factor
#age_group = factor(
#  age_group,
#  level = c( "18-25",
#             "25-29",
#             "29-34",
#             "35-44",
#             "45-54",
#             "55-64",
#             "64-98")
)
#characterizing the sample
clean %>%
ggplot(mapping = aes(x = age_group)) +
geom_bar(width = 0.6)
summary(clean$age)
glimpse(clean)
library(tidyverse)
parse_number("123abc456")
str_extract_all("123abc456","\d")
str_extract_all("123abc456","\\d")
str_extract_all("123abc456","\\d", simplify = T)
str_extract_all("123abc456","\\d", simplify = T) |> paste0(collapse = "")
str_remove_all("123abc456", "\\a")
str_remove_all("123abc456", "[:alpha:]")
str_remove_all("123aBc456", "[:alpha:]")
str_remove_all("123aBc 456", "[:alpha:]")
str_remove_all("123aBc 456", "[:alpha:]|[:punct:]")
str_remove_all("123aBc 456", "[:alpha:]|[:punct:]| ")
?parse_number
1/0
vec = 1:10
new_vec = 1:10
for(i in 0:11){
tryCatch({
new_vec[i] = vec[i] + 1
},
error = \(e){
message(paste("Error occured at iteration", i))
},
warning = \(w){
message(paste("Warning occured at iteration", i))
})
}
new_vec
new_vec[0]
?tryCatch
rbinom(1,1,.5)
rbinom(1,1,.5)
rbinom(1,1,.5)
rbinom(1,1,.5)
1 + "a"
vec = 1:10
new_vec = 1:10
for(i in 1:10){
tryCatch({
s = rbinom(1,1,0.5)
if(s == 0){
new_vec[i] = vec[i] + "a"
} else {
new_vec[i] = vec[i] + 1
}
},
error = \(e){
message(paste("Error occured at iteration", i))
},
warning = \(w){
message(paste("Warning occured at iteration", i))
})
}
new_vec
f.nonmatched$distance[which(df.nonmatched$distance == min(df.nonmatched$distance))]
vec = 1:10
new_vec = 1:10
for(i in 1:10){
tryCatch({
s = rbinom(1,1,0.5)
if(s == 0){
new_vec[i] = vec[i] + "a"
} else {
new_vec[i] = vec[i] + 1
}
},
error = message(paste("Error occured at iteration", i))},
vec = 1:10
new_vec = 1:10
for(i in 1:10){
tryCatch({
s = rbinom(1,1,0.5)
if(s == 0){
new_vec[i] = vec[i] + "a"
} else {
new_vec[i] = vec[i] + 1
}
},
error = message(paste("Error occured at iteration", i)),
warning = \(w){
message(paste("Warning occured at iteration", i))
})
}
vec = 1:10
new_vec = 1:10
for(i in 1:10){
tryCatch({
s = rbinom(1,1,0.5)
if(s == 0){
new_vec[i] = vec[i] + "a"
} else {
new_vec[i] = vec[i] + 1
}
},
error = message(paste("Error occured at iteration", i)),
warning = message(paste("Warning occured at iteration", i))
)
}
vec = 1:10
new_vec = 1:10
for(i in 1:10){
tryCatch({
s = rbinom(1,1,0.5)
if(s == 0){
new_vec[i] = vec[i] + "a"
} else {
new_vec[i] = vec[i] + 1
}
},
error = \(i){
message(paste("Error occured at iteration", i))
},
warning = \(i){
message(paste("Warning occured at iteration", i))
})
}
vec = 1:10
new_vec = 1:10
for(i in 1:10){
tryCatch({
s = rbinom(1,1,0.5)
if(s == 0){
new_vec[i] = vec[i] + "a"
} else {
new_vec[i] = vec[i] + 1
}
},
error = \(e){
message(paste("Error occured at iteration", i))
},
warning = \(w){
message(paste("Warning occured at iteration", i))
})
}
library(tidyverse)
library(rio)
library(sjmisc)
here::i_am("C:/Users/d-hol/Dropbox/COVID_Nationscape/WISDOM")
library(haven)
library(here)
files_stem <- list.files('C:/Users/d-hol/Dropbox/Democracy Tracker - UCLA team/outputs')
files <- paste0('C:/Users/d-hol/Dropbox/Democracy Tracker - UCLA team/outputs/',files_stem[grep('ns',files_stem)],'/',files_stem[grep('ns',files_stem)],'.dta')
files <- files[-c(39,55,80,81)] # Remove Parallel Studies 55
files
wave7 = "C:/Users/d-hol/Dropbox/COVID_Nationscape/WISDOM/wisdom_survey_wave7_cdc.dta"
wave8 = "C:/Users/d-hol/Dropbox/COVID_Nationscape/WISDOM/wisdom_survey_wave8_preview_cdc.dta"
files = c(wave7, wave8)
this_wave = 2
lag = 1
df <- zap_labels(read_stata(files[this_wave - lag])) # Removing labels, causing issues
df$wave <- this_wave - lag
for(i in (this_wave - (lag-1)):length(files)){ # Remove add -1 to lag, length(files) to end
temp <- zap_labels(read_dta(files[i]))
temp$wave <- i
df <- add_rows(df, temp)
print(i)
}
?bind_rows
?vctrs::vec_rbind
for(i in (this_wave - (lag-1)):length(files)){ # Remove add -1 to lag, length(files) to end
temp <- zap_labels(read_dta(files[i]))
temp$wave <- i
df <- vctrs::vec_rbind(df, temp, .ptype = df)
print(i)
}
for(i in (this_wave - (lag-1)):length(files)){ # Remove add -1 to lag, length(files) to end
temp <- zap_labels(read_dta(files[i]))
temp$wave <- i
df <- vctrs::vec_rbind(df, temp, .ptype = temp)
print(i)
}
for(i in (this_wave - (lag-1)):length(files)){ # Remove add -1 to lag, length(files) to end
temp <- zap_labels(read_dta(files[i]))
temp$wave <- i
df <- vctrs::vec_rbind(df, temp)
print(i)
}
class(df$naics_level1)
df <- zap_labels(read_stata(files[this_wave - lag])) |> # Removing labels, causing issues
mutate(across(c("demo_household_income","naics_level1","naics_level2"), as.character))
df$wave <- this_wave - lag
for(i in (this_wave - (lag-1)):length(files)){ # Remove add -1 to lag, length(files) to end
temp <- zap_labels(read_dta(files[i]))
temp$wave <- i
df <- vctrs::vec_rbind(df, temp)
print(i)
}
vars <- names(read_stata(files[this_wave]))
vars <- vars[-which(vars %in% c('weight','weight_2020','weight_both'))]
dat <- as.data.frame(df)
for(i in 1:length(vars)){
if(!is.numeric(dat[,vars[i]])){
next
} else {
print(vars[i])
tab <- prop.table(table(wave=dat$wave, dat[,vars[i]]),1)
#print(round(tab, 3))
print(plot(na.omit(tab), main = vars[i]))
}
readline(prompt="Press [enter] to continue")
}
glimpse(dat)
vars
vars <- names(read_stata(files[this_wave]))
head(vars)
dat <- as.data.frame(df)
for(i in 1:length(vars)){
if(!is.numeric(dat[,vars[i]])){
next
} else {
print(vars[i])
tab <- prop.table(table(wave=dat$wave, dat[,vars[i]]),1)
#print(round(tab, 3))
print(plot(na.omit(tab), main = vars[i]))
}
readline(prompt="Press [enter] to continue")
}
vars <- names(read_stata(files[this_wave]))
vars <- vars[-which(vars %in% c('weights'))]
dat <- as.data.frame(df)
for(i in 1:length(vars)){
if(!is.numeric(dat[,vars[i]])){
next
} else {
print(vars[i])
tab <- prop.table(table(wave=dat$wave, dat[,vars[i]]),1)
#print(round(tab, 3))
print(plot(na.omit(tab), main = vars[i]))
}
readline(prompt="Press [enter] to continue")
}
wave7_in = zap_labels(read_stata(files[2]))
table(wave7_in$Q15x_kid_mental_health_better)
table(wave7_in$Q15x_kid_phys_health_better)
wave8 = "C:/Users/d-hol/Dropbox/COVID_Nationscape/WISDOM/wisdom_survey_wave8_preview_cdc.dta"
library(haven)
library(tidyverse)
library(sjmisc)
wave_8 = zap_labels(read_stata(wave8))
table(wave_8$bivalent_adult)
table(wave_8$surge_exp)
table(wave_8$bivalent_kid)
table(wave_8$bivalent_exp_control)
table(wave_8$bivalent_exp_kid_control)
table(wave_8$bivalent_exp_kid_vacc)
table(wave_8$CDC_howmany_phys_days)
summary(wave_8$CDC_howmany_phys_days)
head(wave_8$CDC_howmany_phys_days)
head(wave_8$Q31x_last30_feelbad)
head(wave_8$ladder)
table(wave_8$household_gun_owner)
prop.table(table(wave_8$household_gun_owner))
prop.table(table(wave_8$Q72x_perceive_gunowner))
summary(wave_8$Q72x_perceive_abortion)
summary(wave_8$Q72x_perceive_gunowner)
sum(wave_8$Q72x_perceive_abortion == 100)
names(mtcars)
mod1 = lm(mpg ~ wt, mtcars)
mod2 = lm(mpg ~ wt + hp, mtcars)
anova_out = anova(mod1, mod2)
anova_out$`Pr(>F)`
anova_out$`Pr(>F)`[2]
wave8 = "C:/Users/d-hol/Dropbox/COVID_Nationscape/WISDOM/wisdom_survey_wave8_preview_cdc.dta"
wave_8 = zap_labels(read_stata(wave8))
library(haven)
library(tidyverse)
library(sjmisc)
wave_8 = zap_labels(read_stata(wave8))
library(tidyverse)
library(rio)
rep_data = import("C:/Users/d-hol/Dropbox/Democracy Tracker - UCLA team/code/representativeness/outputs/data_out_v3.csv")
glimpse(rep_data)
library(tidyverse)
library(patchwork)
library(ggrepel)
library(fixest)
library(scales)
load("demand.RData")
setwd("C:/Users/d-hol/Dropbox/311/replication")
load("demand.RData")
glimpse(demand)
sf_diff_2011 = demand |>
filter(city_service == "San Francisco - Street and Sidewalk Cleaning",
open_month == 12, open_year == 2011)
pull(sf_diff_2011, poor_demand)
pull(sf_diff_2011, pct_rich)
pull(sf_diff_2011, pct_poor)
sf_diff_2011 = demand |>
filter(city_service == "San Francisco - Street and Sidewalk Cleaning",
open_month == 12, open_year == 2011) |>
select(poor_demand, pct_poor, pct_rich)
sf_diff_2018 = demand |>
filter(city_service == "San Francisco - Street and Sidewalk Cleaning",
open_month == 3, open_year == 2018) |>
select(poor_demand, pct_poor, pct_rich)
bind_rows(sf_diff_2011, sf_diff_2018)
bind_rows(sf_diff_2011, sf_diff_2018) |>
mutate(month_year = c("12-2011","03-2018"))
## Loading data
load("dta.RData")
race_wait_across <- feols(log_wait_time ~ factor(white_third) |
city^open_month^open_year, data = dta,
cluster = "geo")
inc_wait_across <- feols(log_wait_time ~ factor(inc_third) |
city^open_month^open_year, data = dta,
cluster = "geo")
race_percent = race_wait_across$coefficients["factor(white_third)1"]
inc_percent  = inc_wait_across$coefficients["factor(inc_third)1"]
avg_wait = mean(dta$wait_time, na.rm = T)
cat("neighborhoods in the top terciles of the city’s white population and income distribution receive about ",
round(min(race_percent,inc_percent)*100,1),"-",
round(max(race_percent,inc_percent)*100,1),"% faster response times - a difference of about ",
round(avg_wait*min(race_percent,inc_percent), 0),"-",
round(avg_wait*max(race_percent,inc_percent), 0),
" hours relative to the average wait time - than do neighborhoods in the bottom terciles", sep = "")
13/22
race_wait_across_decile <- feols(log_wait_time ~ factor(white_decile) |
city^open_month^open_year, data = dta,
cluster = "geo")
race_decile_days = race_wait_across_decile$coefficients["factor(white_decile)1"] * avg_wait / 24
cat("For race in particular, these gaps are even larger when comparing the very top and very bottom",
"deciles (as large as a", round(race_decile_days,0), "day difference relative to the average)")
